﻿var VideoNetSchoolCore = {
    Form: {},
    Dialog: {},
    JQGrid: {},
    DView: {}
};


VideoNetSchoolCore.Form = {

    PostPageSync: function(url, div, data) {
        if (data == undefined)
            data = {};
        $.ajax({
            type: "Post",
            url: url,
            data: data,
            cache: false,
            async: false,
            beforeSend: function(xhr) {
                $(div).html("<img src ='../../Images/loading.gif' alt='loading'  />");
            },
            success: function(response) {
                if (response.indexOf("HasLogOut") < 0) {
                    $(div).html(response);
                }
                else
                    alert("请重新登录！");
            },
            error: function(xml, status) {
                $(div).html(xml.responseText);
            }
        });
    },
    PostData: function(url, data, afterRender, onError) {
        if (data == undefined)
            data = {};
        $.ajax({
            type: "Post",
            url: url,
            data: data,
            cache: false,
            beforeSend: function(xhr) {
            },
            success: function(response) {
                if (afterRender != undefined) {
                    afterRender(response);
                }
                //               
                //                if (response.ResultValue == true) {
                //                    if (afterRender != undefined) {
                //                        afterRender(response);
                //                    }
                //                }
                //                else {
                //                    if (onError != undefined) {
                //                        onError(response);
                //                    }
                //                }
            },
            error: function(xml, status) {
                alert(xml.responseText);
            }
        });
    },
    Request: function(paras) {
        var url = location.href;
        var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
        var paraObj = {}
        for (i = 0; j = paraString[i]; i++) {
            paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
        }
        var returnValue = paraObj[paras.toLowerCase()];
        if (typeof (returnValue) == "undefined") {
            return "";
        } else {
            return returnValue;
        }

    },



    ShowMaskLayer: function() {
        $("#Masklayer").show();
    },
    CloseMaskLayer: function() {
        $("#Masklayer").hide();
    },

    PostPage: function(url, div, data, afterRender) {
        if (data == undefined)
            data = {};
        $.ajax({
            type: "Post",
            url: url,
            data: data,
            cache: false,
            beforeSend: function(xhr) {
                $(div).html("<img src ='../../Images/loading.gif' alt='loading'  />");
                //增加遮罩层显示
                VideoNetSchoolCore.Form.ShowMaskLayer();
                //alert("ff");
            },
            success: function(response) {

                $(div).html(response);
                if (afterRender != undefined) {
                    afterRender(response);
                }
                //关闭遮罩层
                VideoNetSchoolCore.Form.CloseMaskLayer();

            },
            error: function(xml, status) {
                $(div).html(xml.responseText);
                VideoNetSchoolCore.Form.CloseMaskLayer();
            }
        });
    },
    GetDivFormData: function(div) {
        var data = {};
        div.find("[DataField]").each(function(i) {
            data[$(this).attr("DataField")] = VideoNetSchoolCore.Form.GetControlData($(this));
        });
        return data;
    },
    SetDivFormData: function(div, jsonData) {
        div.find("[DataField]").each(function(i) {
            var dataField = $(this).attr("DataField");
            if (dataField != undefined) {
                VideoNetSchoolCore.Form.SetControlData($(this), jsonData[dataField]);
            }
        });
    },
    SetControlData: function(control, data) {
        switch ($(control).attr("tagName")) {
            case "INPUT":
                {
                    data += $(control).val(data);
                }
                break;
            case "SPAN":
                {
                    data += $(control).text(data);
                }
                break;
            default:
                data += $(control).val(data);
                break;
        }

    },
    GetControlData: function(control) {
        var data = "";
        switch ($(control).attr("tagName")) {
            case "TEXTAREA":
            case "INPUT":
                {
                    if ($(control).attr("type") == "checkbox") {
                        data += $(control).attr("checked");
                    }
                    else
                        data += $(control).val();

                }
                break;
            case "SPAN":
                {
                    data += $(control).text();
                }
                break;
            case "SELECT":
                {
                    data += $(control).val();
                }
                break; default:
                data += $(control).val();
                break;
        }
        return VideoNetSchoolCore.Form.ConvertToJson(data);
    },
    ConvertToJson: function(s) {
        s = s.replace(/(\\|\"|\')/g, "\\$1")
                    .replace(/\n|\r|\t/g,
                    function() {
                        var a = arguments[0];
                        return (a == '\n') ? '\\n' :
                           (a == '\r') ? '\\r' :
                           (a == '\t') ? '\\t' : ""
                    });
        return s;
    },
    ConverArryToString: function(s) {
        var newstring = '';
        if (s.length > 0) {
            for (var i = 0; i < s.length; i++) {
                newstring = newstring + s[i] + ",";
            }
        } else {
            newstring = s;
        }
        return newstring;
    },
    SetJqGrid: function(div, tableID, op) {
        $(div).empty();
        $(div).html("<table id='" + tableID + "'> </table>  <div id='" + tableID + "Page'></div>");
        return $("#" + tableID).jqGrid(op);
    },
    GetHfControlValue: function(o) {
        return $("#" + o).val();
    },
    SetHfControlValue: function(o, rowid) {
        $("#" + o).val(rowid);
    },
    QueryNormalForm: function(code, func) {

    }

};

 

 

 

Date.prototype.format = function(formatter) {
    if (!formatter || formatter == "") {
        formatter = "yyyy-MM-dd";
    }
    var year = this.getYear().toString();
    var month = (this.getMonth() + 1).toString();
    var day = this.getDate().toString();
    var yearMarker = formatter.replace(/[^y|Y]/g, '');
    if (yearMarker.length == 2) {
        year = year.substring(2, 4);
    }
    var monthMarker = formatter.replace(/[^m|M]/g, '');
    if (monthMarker.length > 1) {
        if (month.length == 1) {
            month = "0" + month;
        }
    }
    var dayMarker = formatter.replace(/[^d]/g, '');
    if (dayMarker.length > 1) {
        if (day.length == 1) {
            day = "0" + day;
        }
    }
    return formatter.replace(yearMarker, year).replace(monthMarker, month).replace(dayMarker, day);
}
Date.parseString = function(dateString, formatter) {
    var today = new Date();
    if (!dateString || dateString == "") {
        return today;
    }
    if (!formatter || formatter == "") {
        formatter = "yyyy-MM-dd";
    }
    var yearMarker = formatter.replace(/[^y|Y]/g, '');
    var monthMarker = formatter.replace(/[^m|M]/g, '');
    var dayMarker = formatter.replace(/[^d]/g, '');
    var yearPosition = formatter.indexOf(yearMarker);
    var yearLength = yearMarker.length;
    var year = dateString.substring(yearPosition, yearPosition + yearLength) * 1;
    if (yearLength == 2) {
        if (year < 50) {
            year += 2000;
        }
        else {
            year += 1900;
        }
    }
    var monthPosition = formatter.indexOf(monthMarker);
    var month = dateString.substring(monthPosition, monthPosition + monthMarker.length) * 1 - 1;
    var dayPosition = formatter.indexOf(dayMarker);
    var day = dateString.substring(dayPosition, dayPosition + dayMarker.length) * 1;
    return new Date(year, month, day);
}




VideoNetSchoolCore.JQGrid = {
    CurrentGrid: null,
    GetJQGridConfg: function(container) {
        if (container == undefined) {
            container = "*";
        }
        var configData = $(container).find("[DataField='JQGridConfig']").val();
        var jsonConfigData = eval('(' + configData + ')');
        return jsonConfigData;
    },
    JQGridSetLocal: function(container, paras) {

        var jsonConfigData = VideoNetSchoolCore.JQGrid.GetJQGridConfg(container);
        $.each(paras, function(key, value) {
            jsonConfigData.MyJQGridParameters[key] = value;
        });

        jsonConfigData.MyJQGridParameters.beforeRequest = function() {
            VideoNetSchoolCore.Form.ShowMaskLayer();
        };
        jsonConfigData.MyJQGridParameters.loadError = function() {
            alert("Error");
        };
        var tableID = "#" + jsonConfigData.TableID;
        var pagerID = "#" + jsonConfigData.PagerID;

        jQuery(tableID).jqGrid(jsonConfigData.MyJQGridParameters);
        if (jsonConfigData.EnabledPager) {
            jQuery(tableID).jqGrid('navGrid', pagerID, jsonConfigData.MyJQGridPager);
        }

        if (jsonConfigData.EnabledFilterToolBar)
            jQuery(tableID).jqGrid('filterToolbar');


        //jQuery.setGridParam({ datatype: "json" });
        //jQuery(tableID).trigger("reloadGrid");
    },
    JQGridReload: function(args, tableID) {
        $(tableID).setGridParam({ page: 1 });
        $.each(args, function(key, value) {
            $(tableID).setPostDataItem(key, value);
        });
        $(tableID).trigger("reloadGrid");
    },
    InitCheckBox: function(table) {
        var ids = $(table).jqGrid('getDataIDs');
        for (var i = 0; i < ids.length; i++) {
            var cbx = "<input type='checkbox' id='cbx_" + ids[i] + "'  value='" + ids[i] + "' />";
            $(table).jqGrid('setRowData', ids[i], { SelectRow: cbx });
        }
        $("#jqgh_SelectRow").removeAttr("class");
        $("#jqgh_SelectRow").unbind();
        $("#jqgh_SelectRow").prepend("<input type='checkbox' id='cbx_selectAll'  onClick='VideoNetSchoolCore.JQGrid.CheckBoxToggle(this);' />");
    },
    CheckBoxToggle: function() {
        $.each($("[aria-describedby='IntegralProductGrid_SelectRow']"), function() {
            $(this).find("input[type='checkbox']").attr("checked", $("#cbx_selectAll").attr("checked"));
        });
    },
    GetSelectedIDs: function() {
        var ids = new Array();
        $.each($("[aria-describedby='IntegralProductGrid_SelectRow']"), function() {
            var cbx = $(this).find("input[type='checkbox']");
            if (cbx.attr("checked") == true)
                ids.push(cbx.val());
        });
        return ids;
    }
};

$.fn.GetJQGridSelectRowID = function() {
    var container = $("#" + $(this).attr("GridContainer"));
    var val = $(container).find("[DataField='CurrentRowid']").val();
    if (val == undefined || val == "")
        return null;
    else
        return val;


}

$.fn.MyJQGrid = function(op) {
    VideoNetSchoolCore.JQGrid.JQGridSetLocal(this, op);
};
$.fn.MyJQGridOption = function() {


    return VideoNetSchoolCore.JQGrid.GetJQGridConfg(this);
};



VideoNetSchoolCore.Dialog = {
    DefaultDialog: null,
    DefaultOptions: {
        autoOpen: false,
        modal: true,
        bgiframe: true,
        height: 400,
        width: 750,
        title: "",
        close: function(event, ui) {
            VideoNetSchoolCore.Dialog.DefaultDialog.empty();
        }
    },
    InitDialog: function() {

        $("#dialogSystem").dialog(VideoNetSchoolCore.Dialog.DefaultOptions);
        VideoNetSchoolCore.Dialog.DefaultDialog = $("#dialogSystem");
    },
    OpenDialog: function(options) {
        if (options != undefined) {
            $.each(VideoNetSchoolCore.Dialog.DefaultOptions, function(key, value) {
                if (options[key] == null)
                    options[key] = value;
            });
            VideoNetSchoolCore.Dialog.DefaultDialog.dialog("option", options);
        }
        VideoNetSchoolCore.Dialog.DefaultDialog.dialog("open");
    },
    CloseDialog: function() {
        VideoNetSchoolCore.Dialog.DefaultDialog.dialog("close");
    }




};